var express = require('express');
var router = express.Router();
var { userModel, chatHistoryModel } = require('../model/model')
/* GET home page. */
// 获取聊天列表
router.post('/getChatList', async (req, res, next) => {
    let { _id } = req.body
    // console.log("123",_id);
    let queryAll = [{}]
    if (_id) {
        queryAll = [{ senderId: _id }, { recipientId: _id }]
    }
    let ChatList = await chatHistoryModel.find({ $or: queryAll }).sort({ dateTime: -1 }).populate('senderId recipientId')
    // console.log("123", ChatList)
    res.send({
        code: 200,
        ChatList
    })

    
});

// 获取与某一个人的聊天记录
router.post('/getchat-history', async (req, res, next) => {
    let { myId, yourId } = req.body
    const query = {
        $and: [
            {
                $or: [
                    { senderId: myId },
                    { senderId: yourId }
                ]
            },
            {
                $or: [
                    { recipientId: myId },
                    { recipientId: yourId }
                ]
            }
        ]
    };
    let ChatList = await chatHistoryModel.find(query).sort({ dateTime: 1 }).populate('senderId recipientId')
    res.send({
        code: 200,
        ChatList
    })
});

module.exports = router;
